import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:oli/bean/StatisticsBean.dart';

import 'logic.dart';

class StatisticPage extends StatelessWidget {
  const StatisticPage({super.key});
  @override
  Widget build(BuildContext context) {
    final logic = Get.find<StatisticLogic>();
    final state = logic.state;
    return Container(
      margin: const EdgeInsets.all(12),
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.2),
            spreadRadius: 1,
            blurRadius: 3,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              const Text(
                '年份',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  color: Colors.blue,
                ),
              ),
              const SizedBox(width: 10),
              Obx(
                () => DropdownButton<String>(
                  value: state.selectedYear.value,
                  items:
                      state.years.map((year) {
                        return DropdownMenuItem<String>(
                          value: year,
                          child: Text(year),
                        );
                      }).toList(),
                  onChanged: (newValue) {
                    state.selectedYear.value = newValue;
                  },
                ),
              ),
            ],
          ),
          const SizedBox(height: 10),
          // 根据选择的年份展示相应的统计数据
          Obx(
            () =>
                state.statistics.isNotEmpty
                    ? buildStatisticsForYear(state.selectedYear.value)
                    : Container(),
          ),
        ],
      ),
    );
  }

  Widget buildStatisticsForYear(String? year) {
    if (year == null || year.isEmpty) {
      return Container();
    }
    final logic = Get.find<StatisticLogic>();
    final state = logic.state;
    final selectedStat = state.statistics.firstWhere(
      (stat) => stat.year == year,
      orElse:
          () => StatisticsBean(
            timeRange: '',
            averageFuelConsumption: 0,
            averageTripDistance: 0,
            averageCostPerKm: 0,
            totalMileage: 0,
            totalCost: 0,
            totalFuel: 0,
            year: '',
          ),
    );
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            statItemWithBackground(
              label: '平均油耗',
              value: '${selectedStat.averageFuelConsumption}升/百公里',
              backgroundColor: Colors.greenAccent.withOpacity(0.2),
              textColor: Colors.green,
            ),
            statItemWithBackground(
              label: '平均行程',
              value: '${selectedStat.averageTripDistance}公里/天',
              backgroundColor: Colors.orangeAccent.withOpacity(0.2),
              textColor: Colors.orange,
            ),
            statItemWithBackground(
              label: '平均油费',
              value: '${selectedStat.averageCostPerKm}元/公里',
              backgroundColor: Colors.purpleAccent.withOpacity(0.2),
              textColor: Colors.purple,
            ),
          ],
        ),
        const SizedBox(height: 12),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            statItemWithBackground(
              label: '累计行程',
              value: '${selectedStat.totalMileage}公里',
              backgroundColor: Colors.lightBlueAccent.withOpacity(0.2),
              textColor: Colors.lightBlue,
            ),
            statItemWithBackground(
              label: '累计油费',
              value: '${selectedStat.totalCost}元',
              backgroundColor: Colors.redAccent.withOpacity(0.2),
              textColor: Colors.red,
            ),
            statItemWithBackground(
              label: '累计加油',
              value: '${selectedStat.totalFuel}升',
              backgroundColor: Colors.yellowAccent.withOpacity(0.2),
              textColor: Colors.yellow,
            ),
          ],
        ),
      ],
    );
  }

  Widget statItemWithBackground({
    required String label,
    required String value,
    required Color backgroundColor,
    required Color textColor,
  }) {
    return Expanded(
      child: Container(
        padding: const EdgeInsets.all(8),
        margin: const EdgeInsets.symmetric(horizontal: 6),
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: BorderRadius.circular(6),
        ),
        child: Column(
          children: [
            Text(label, style: TextStyle(color: textColor, fontSize: 12)),
            const SizedBox(height: 3),
            Text(
              value,
              style: TextStyle(
                color: textColor,
                fontSize: 14,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
